#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>

int main(int argc, char **argv)
{
    if (argc < 0) {
        printf("a.out {port}");
        return -1;
    }

    int fd = socket(AF_INET, SOCK_DGRAM, 0);
    if(fd == -1)
    {
        perror("socket");
        exit(0);
    }


    struct sockaddr_in seraddr;
    seraddr.sin_family = AF_INET;
    unsigned short port = port;
    sscanf(argv[1], "%hu", &port);
    seraddr.sin_port = htons(port);
    inet_pton(AF_INET, "127.0.0.1", &seraddr.sin_addr.s_addr);

    char buf[1024];
    //char ipbuf[64];
    //struct sockaddr_in cliaddr;
    //int len = sizeof(cliaddr);
    int num = 0;

    while(1)
    {
        sprintf(buf, "hello, I am udp client %d....\n", num++);
        sendto(fd, buf, strlen(buf)+1, 0, (struct sockaddr*)&seraddr, sizeof(seraddr));
        memset(buf, 0, sizeof(buf));
//        recvfrom(fd, buf, sizeof(buf), 0, NULL, NULL);
//        printf("recvfrom server say: %s\n", buf);
        sleep(1);
    }
    close(fd);
    return 0;
}